const log = console.log.bind(console)
//给一个开关设定
let flay = false
//拿到音乐播放器
let audio = document.querySelector('audio')
//拿到播放按钮
let kaishi = document.querySelector('button#play')
//拿到主图
let picture = document.querySelector('img.picture')
//拿到下一首按钮
let forward = document.querySelector('button#forward')
//拿到上一首按钮
let backward = document.querySelector('button#backward')
//拿到音量按键
let volume = document.querySelector('button#volume')
//拿到播放方式按钮
let reorder = document.querySelector('button#reorder')
//拿到放置总时间的位置
let ltime = document.querySelector('span#ltime')
//拿到现在播放的时间的位置
let ntime = document.querySelector('span#ntime')
//拿到进度条位置
let leimo = document.querySelector('meter#leimo')
//歌曲列表用数组装一下
let quku = ['阿婆说','河清海晏','了']
//更换按钮
const BfaNiu = function () {
    document.querySelector('i#played').className ='fa fa-pause'
}
const StopaNiu = function () {
    document.querySelector('i#played').className = 'fa fa-play'
}
const VolumeAnNiu = () => {
    document.querySelector('i#vol').className = 'fa fa-volume-off'
}
const VoNiu = () => {
    document.querySelector('i#vol').className = 'fa fa-volume-up'
}
const DanQu = () => {
    document.querySelector('i#danqu').className = 'fa fa-refresh'
}
const Shunxu = () => {
   document.querySelector('i#danqu').className = 'fa fa-reorder'
}
//执行播放和关闭
const bof = function () {
    kaishi.addEventListener('click',function () {
        if (!flay) {
            audio.play()
            BfaNiu()
            flay = true
        } else {
            audio.pause()
            StopaNiu()
            flay = false
        }
    })
}

//下一首
const next = function () {
    forward.addEventListener('click',function () {
        let a = picture.getAttribute('data-index')
        picture.setAttribute('data-index',jia(a))
        picture.src = `图片/${jia(a)}.jpg`
        picture.alt = quku[jia(a)]
        audio.src = `music/${quku[jia(a)]}.mp3`
        if (flay) {
            audio.play()
        } else {
            audio.pause()
        }
    })
}
//上一首
const last = () => {
    backward.addEventListener('click',function () {
        let a = picture.getAttribute('data-index')
        picture.setAttribute('data-index',jian(a))
        picture.src = `图片/${jian(a)}.jpg`
        picture.alt = quku[jian(a)]
        audio.src = `music/${quku[jian(a)]}.mp3`
        if (flay) {
            audio.play()
        } else {
            audio.pause()
        }
    })
}
//函数判断是否能加一
const jia = (x) => {
    if (x < quku.length - 1) {
        x++
    } else {
        x = 0
    }
    return x
}
//函数判断是否减一
const jian = (x) => {
    if (x < 1) {
        x = quku.length - 1
    } else {
        x--
    }
    return x
}

//静音与否
const yinl = () => {
    volume.addEventListener('click',function () {
        if (audio.volume > 0) {
            VolumeAnNiu()
            return audio.volume = 0
        } else {
            VoNiu()
            return audio.volume = 1
        }
    })
}

//切换播放方式
const bofanfs = () => {
    reorder.addEventListener('click',function () {
        let a = reorder.className
        if (a === 'one') {
            Shunxu()
            reorder.className = ' '
        } else {
            DanQu()
            reorder.className = 'one'
        }
    })
}
//单曲循环
const forone = () => {
    audio.addEventListener('ended',function () {
        let a = reorder.className
        if (a === 'one') {
            audio.play()
        } else {
            let s = picture.getAttribute('data-index')
            picture.setAttribute('data-index',jia(s))
            picture.src = `图片/${jia(s)}.jpg`
            picture.alt = quku[jia(s)]
            audio.src = `music/${quku[jia(s)]}.mp3`
            audio.play()
        }
    })
}

//秒转成分钟
const mzf = function (time) {
    let m = String(Math.floor(time / 60))
    let s = String((time % 60).toFixed())
    if (s.length < 2) {
        s = '0' + s
    }
    return m + ':' + s
}
//拿到总时间
const longtimg = () => {
    audio.addEventListener('canplay', function () {
        let a = audio.duration
        let s = mzf(a)
        document.querySelector("meter").max = a
        ltime.innerHTML = s
    })
}
//拿到现在播放的时间
const stime = function () {
    let a = audio.currentTime
    let s = mzf(a)
    ntime.innerHTML = s
}
// 每一秒查询播放时间
const nowl = function () {
    setInterval(stime,1000)
}
//拿到进度条时间
const newlong = function () {
    let a = audio.currentTime.toFixed()
    leimo.value = a
}
//显示进度条时间
const howlong = function () {
    setInterval(newlong,1000)
}

//统一入口
const _min = () => {
    bofanfs()
    bof()
    next()
    last()
    yinl()
    forone()
    howlong()
    nowl()
    longtimg()
}
_min()